package com.gbits.app.finance.api;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.gbits.app.finance.service.CostOrgSubjectMapperService;
import com.gbits.app.finance.service.DeptSubjectMapperService;
import com.gbits.app.finance.service.SubjectBusinessMapperService;
import com.gbits.app.finance.service.SubjectService;

/**
 * 字符串转换
 * @author wangqt
 *
 */
public abstract class ItemFinanceConvert implements FinanceConvert<String> {
	
	public static final String SUBJECT_SPLIT = " - ";
	
	@Autowired
	private CostOrgSubjectMapperService costOrgSubjectMapperService;
	@Autowired
	private DeptSubjectMapperService deptSubjectMapperService;
	@Autowired
	private SubjectService subjectService;
	
	private SubjectBusinessMapperService subjectBusinessMapperService;
	
	public SubjectBusinessMapperService getSubjectBusinessMapperService() {
		return subjectBusinessMapperService;
	}
	public void setSubjectBusinessMapperService(
			SubjectBusinessMapperService subjectBusinessMapperService) {
		this.subjectBusinessMapperService = subjectBusinessMapperService;
	}
	public CostOrgSubjectMapperService getCostOrgSubjectMapperService() {
		return costOrgSubjectMapperService;
	}
	public void setCostOrgSubjectMapperService(
			CostOrgSubjectMapperService costOrgSubjectMapperService) {
		this.costOrgSubjectMapperService = costOrgSubjectMapperService;
	}
	public DeptSubjectMapperService getDeptSubjectMapperService() {
		return deptSubjectMapperService;
	}
	public void setDeptSubjectMapperService(
			DeptSubjectMapperService deptSubjectMapperService) {
		this.deptSubjectMapperService = deptSubjectMapperService;
	}
	public SubjectService getSubjectService() {
		return subjectService;
	}
	public void setSubjectService(SubjectService subjectService) {
		this.subjectService = subjectService;
	}
	private String getString(String key, Map<String, Object> map) {
		Object value = map.get(key);
		if (null != value) {
			return String.valueOf(value);
		}
		return null;
	}
	/**
	 * 获取成本中心
	 * @param map
	 * @return
	 */
	protected String getCostOrgName(Map<String, Object> map) {
		return this.getString("cost_organ_name", map);
	}
	/**
	 * 获取责任中心
	 * @param map
	 * @return
	 */
	protected String getOrgName(Map<String, Object> map) {
		return this.getString("organ_name", map);
	}
	/**
	 * 获取银行账号
	 * @param map
	 * @return
	 */
	protected String getPayBankNo(Map<String, Object> map) {
		return this.getString("pay_bank_no", map);
	}
	/**
	 * 摘要
	 * @param map
	 * @return
	 */
	protected String getUsage(Map<String, Object> map) {
		return this.getString("nusage", map);
	}
	/**
	 * 获取收款人姓名
	 * @param map
	 * @return
	 */
	protected String getAccountName(Map<String, Object> map) {
		return this.getString("account_name", map);
	}
	/**
	 * 获取OA科目 
	 * @param map
	 * @return
	 */
	protected String getSubjectName(Map<String, Object> map) {
		return this.getString("subject_name", map);
	}
	/**
	 * 判断是否为公司
	 * @param map
	 * @return
	 */
	protected boolean isCompany(Map<String, Object> map) {
		return "y".equalsIgnoreCase(this.getString("is_company", map));
	}
	/**
	 * 是否含税
	 * @param map
	 * @return
	 */
	protected boolean isCantainTax(Map<String, Object> map) {
		return "y".equalsIgnoreCase(this.getString("is_contain_tax", map));
	}
	/**
	 * 报销金额
	 * @param map
	 * @return
	 */
	protected String getAmount(Map<String, Object> map) {
		return this.getString("amount", map);
	}
	/**
	 * 税额
	 * @param map
	 * @return
	 */
	protected String getTaxAmount(Map<String, Object> map) {
		return this.getString("tax_amount", map);
	}
	/**
	 * 不含税金额
	 * @param map
	 * @return
	 */
	protected String getNoTaxAmount(Map<String, Object> map) {
		return this.getString("no_tax_amount", map);
	}
	
	/**
	 * 公司代码
	 * @param map
	 * @return
	 */
	protected String getCompanyCode(Map<String, Object> map) {
		return this.getString("company_str", map);
	}
	/**
	 * 任证ID
	 * @param map
	 * @return
	 */
	protected String getAccoundId(Map<String, Object> map) {
		return this.getString("account_id", map);
	}
	
	/**
	 * 获取摘要
	 * @param map
	 * @return
	 */
	public abstract String getUsageInfo(Map<String, Object> map);
	
	/**
	 * 获取科目详情
	 * @param map
	 * @return
	 */
	public abstract String getSubjectDetail(Map<String, Object> map);
	/**
	 * 获取借方金额
	 * @param map
	 * @return
	 */
	public abstract String getDebitAmount(Map<String, Object> map);
	
	/**
	 * 获取贷方金额
	 * @param map
	 * @return
	 */
	public abstract String getCreditAmount(Map<String, Object> map);
	
	public String doConvert(Map<String, Object> map) {
		
		if (!this.support(map)) {
			return null;
		}
		String debit = this.getDebitAmount(map);
		if (!StringUtils.isEmpty(debit)) {
			debit = String.format("(%s)", debit);
		}
		return String.format("%-10s\t%-50s\t%s\t%s", this.getUsageInfo(map), this.getSubjectDetail(map), "" + debit, "" + this.getCreditAmount(map));
	}
	
	/**
	 * 获取科目列表
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getSubjectList(Map<String, Object> map) {
		return (List<Map<String, Object>>) map.get("subject_list");
	}
	/**
	 * 获取支付列表
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getPayList(Map<String, Object> map) {
		return (List<Map<String, Object>>) map.get("pay_list");
	}
	
	/**
	 * 获取税额列表
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getTaxList(Map<String, Object> map) {
		return (List<Map<String, Object>>) map.get("tax_list");
	}
	
	/**
	 * 获取所有的税额
	 * @param map
	 * @return
	 */
	public Double getAllTaxAmount(Map<String, Object> map) {
		double value = 0d;
		List<Map<String, Object>> list = this.getTaxList(map);
		for (Map<String, Object> tax : list) {
			if (this.isCantainTax(tax)) {
				value += Double.parseDouble(this.getTaxAmount(tax));
			}
		}
		return value;
	}
	
	public boolean support(Map<String, Object> map) {
		return true;
	}
}
